package com.hnkywl.sts.service.jhgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.jhgl.*;
import com.hnkywl.sts.entity.jhgl.Tpzjjh;
import com.hnkywl.sts.entity.jhgl.Tpzjjhmx;
import com.hnkywl.sts.entity.jhgl.Zgzjjh;
import com.hnkywl.sts.entity.jhgl.Zgzjjhmx;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ZHUCHG
 * Date: 13-4-9
 * Time: 下午9:04
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ZjjhService {
    private ZgzjjhDao zgzjjhDao;
    private ZgzjjhmxDao zgzjjhmxDao;
    private TpzjjhDao tpzjjhDao;
    private TpzjjhmxDao tpzjjhmxDao;
    private ZjjhDao zjjhDao;
    private BmService bmService;

    private TpwzjhmxDao tpwzjhmxDao;

    public String findList(String type, String jhnf, String jhyf, Long gysId,Long sbgsId, Long sbbmId, String zt,String jhbh, int pageNo, int pageSize) {
        //id,计划单号,物资计划类型,物资计划编号,供应商,客户,物资申报公司,支付方式,自购类型,备注,资金计划申报公司,申报人,申报日期,状态
        Page<Map> page = zjjhDao.findList(type, jhnf, jhyf, gysId,sbgsId, sbbmId, zt, jhbh,pageNo, pageSize);
        Page<Map> Allpage = zjjhDao.findList(type, jhnf, jhyf, gysId,sbgsId, sbbmId,jhbh, zt, 1, -1);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(page.getResult());
        Double sbje = 0d;
        if(Allpage.getResult().size()!=0){
            for(Map map : Allpage.getResult()){
                sbje += Double.valueOf((map.get("sbje")==null?0f:map.get("sbje")).toString());
            }
            return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + ",\"footer\":[{\"jhbh\":\"合计:\",\"sbje\":" + sbje + " }]}";
        }else {
            return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
        }
    }

    /**
     * 首页资金计划数
     *
     * @return
     */
    public String findAllZjjh() {
        Integer zjjhNum = 0;
        try {
            Date aDate = new Date(System.currentTimeMillis());
            Calendar calendar = GregorianCalendar.getInstance();
            calendar.setTime(aDate);
            Integer year = calendar.get(Calendar.YEAR);    //获取年
            Integer month = calendar.get(Calendar.MONTH) + 2;   //获取月份，0表示1月份
            return zjjhDao.findZjjhSum( year.toString(), month.toString(), "ybc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zjjhNum.toString();
    }

    public String findPmByTpwzjh(Long id) {
        List<Map> list = tpwzjhmxDao.findPmByTpwzjh(id);
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(list);
        return "{\"rows\":" + json + "}";
    }

    public Zgzjjh findZgzzjh(Long id) {
        return zgzjjhDao.get(id);
    }

    public Tpzjjh findTpzjjh(Long id) {
        return tpzjjhDao.get(id);
    }

    /**
     * 查找与资金计划关联的所有付款金额
     *
     * @param zjjhId
     * @param jhlx
     * @return
     */
    public Double findFkje(Long zjjhId, String jhlx) {
        return zjjhDao.findFkje(zjjhId, jhlx);
    }

    public String findZgzjjhmxByZgzjjh(Long id) {
        Zgzjjh zgzjjh = zgzjjhDao.get(id);
        String json = BaseUtils.fetchCollectionToJson(null, false, zgzjjh.getZgzjjhmxList(), "fkrq", "fkje");
        json = "{\"rows\":[" + json + "],\"footer\":[{\"fkrq\":\"合计\",\"fkje\":" + zgzjjh.getSbje() + "}]}";
        return json;
    }

    public String findTpzjjhmxByTpzjjh(Long id) {
        Tpzjjh tpzjjh = tpzjjhDao.get(id);
        String json = BaseUtils.fetchCollectionToJson(null, false, tpzjjh.getTpzjjhmxList(), "pm.id","pm.name", "hdsl", "fkrq", "fkje");
        json = "{\"rows\":[" + json + "],\"footer\":[{\"fkrq\":\"合计\",\"fkje\":" + tpzjjh.getSbje() + "}]}";
        return json;
    }

    @Transactional
    public void saveZgzjjh(Zgzjjh zgzjjh) {
        zgzjjh.setLastUpdated(new Date());
        zgzjjhDao.save(zgzjjh);
    }

    @Transactional
    public void saveTpzjjh(Tpzjjh tpzjjh) {
        tpzjjh.setLastUpdated(new Date());
        tpzjjhDao.save(tpzjjh);
    }

    @Transactional
    public List<Zgzjjhmx> findZgzjjhmx(Long id) {
        return   this.zgzjjhmxDao.findZgzjjhmxList(id);
    }

    @Transactional
    public void saveZgzjjh(Zgzjjh zgzjjh, List<Zgzjjhmx> zgzjjhmxList) {
        String ssgsbm = zgzjjh.getSbgs().getBm();
        Long ssgsId = zgzjjh.getSbgs().getId();
        zgzjjh.setJhbh(bmService.getZjjhbm("ZGZJJH"));
        if(zgzjjh.getCggs()!=null&&zgzjjh.getCggs().getId()==null){
            zgzjjh.setCggs(null);
        }
        //自购物资计划
        if (zgzjjh.getZgwzjh().getId() == null)
            zgzjjh.setZgwzjh(null);
        //供应商
        if (zgzjjh.getGys().getId() == null)
            zgzjjh.setGys(null);
        //客户
        if (zgzjjh.getKh().getId() == null)
            zgzjjh.setKh(null);
        //担保方
        if (zgzjjh.getDbf().getId() == null)
            zgzjjh.setDbf(null);
        //申报公司
        if (zgzjjh.getSbgs().getId() == null)
            zgzjjh.setSbgs(null);
        //申报部门
        if (zgzjjh.getSbbm().getId() == null)
            zgzjjh.setSbbm(null);
//        if (zgzjjh.getSbjg().getId() == null)
//            zgzjjh.setSbjg(null);
        //支付方式
        if (zgzjjh.getZffs().getId() == null)
            zgzjjh.setZffs(null);
        if (zgzjjh.getMll() != null)
            zgzjjh.setMll(zgzjjh.getMll());
        zgzjjh.setSbrq(new Date());
//        zgzjjh.setSbr(YgService.getCurrentYg());
        zgzjjh.setDateCreated(new Date());
        zgzjjh.setLastUpdated(zgzjjh.getDateCreated());
        zgzjjh.setCreateUser(YgService.getCurrentYg());
        zgzjjh.setCreateOrgan(YgService.getCurrentYg().getZggs());
        zgzjjh.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        //计算申报金额
        zgzjjh.setSbje(sumZgsbje(zgzjjhmxList));
        //保存自购资金计划
        zgzjjhDao.save(zgzjjh);

        if (zgzjjhmxList != null) {
            for (Zgzjjhmx temp : zgzjjhmxList) {
                temp.setZgzjjh(zgzjjh);
                zgzjjhmxDao.save(temp);
            }
        }
    }

    @Transactional
    public void saveTpzjjh(Tpzjjh tpzjjh, List<Tpzjjhmx> tpzjjhmxList) {
        String ssgsbm = tpzjjh.getSbgs().getBm();
        Long ssgsId = tpzjjh.getSbgs().getId();
        tpzjjh.setJhbh(bmService.getZjjhbm("TPZJJH"));
        //自购物资计划
        if (tpzjjh.getTpwzjh().getId() == null)
            tpzjjh.setTpwzjh(null);
        //供应商
        if (tpzjjh.getGys().getId() == null)
            tpzjjh.setGys(null);
        //申报公司
        if (tpzjjh.getSbgs().getId() == null)
            tpzjjh.setSbgs(null);
        //申报部门
        if (tpzjjh.getSbbm().getId() == null)
            tpzjjh.setSbbm(null);
//        if (tpzjjh.getSbjg().getId() == null)
//            tpzjjh.setSbjg(null);
        //支付方式
        if (tpzjjh.getZffs().getId() == null)
            tpzjjh.setZffs(null);
        tpzjjh.setSbrq(new Date());
        tpzjjh.setSbr(YgService.getCurrentYg());
        tpzjjh.setCreateUser(YgService.getCurrentYg());
        tpzjjh.setCreateOrgan(YgService.getCurrentYg().getZggs());
        tpzjjh.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        tpzjjh.setDateCreated(new Date());
        tpzjjh.setLastUpdated(tpzjjh.getDateCreated());
        //计算申报金额
        tpzjjh.setSbje(sumTpzjjh(tpzjjhmxList));
        //保存统配资金计划
        tpzjjhDao.save(tpzjjh);

        if (tpzjjhmxList != null) {
            for (Tpzjjhmx temp : tpzjjhmxList) {
                temp.setTpzjjh(tpzjjh);
                tpzjjhmxDao.save(temp);
            }
        }
    }

    @Transactional
    public void editZgzjjh(Zgzjjh zgzjjh, List<Zgzjjhmx> zgzjjhmxList) {
        Zgzjjh z = zgzjjhDao.get(zgzjjh.getId());
        z.setJhnf(zgzjjh.getJhnf());
        z.setJhyf(zgzjjh.getJhyf());
//        z.setSbje(zgzjjh.getSbje());
        z.setSbgs(zgzjjh.getSbgs());
        z.setSbbm(zgzjjh.getSbbm());
        z.setSbr(zgzjjh.getSbr());
        z.setMll(zgzjjh.getMll());
        z.setZffs(zgzjjh.getZffs());
        z.setLastUpdated(new Date());
        z.setSbje(sumZgsbje(zgzjjhmxList));;
        //保存自购资金计划
        zgzjjhDao.save(z);

        for (Zgzjjhmx mx : z.getZgzjjhmxList()) {
            mx.setLastUpdated(new Date());
            mx.setDeleted(1);
            zgzjjhmxDao.save(mx);
        }

        if (zgzjjhmxList != null) {
            for (Zgzjjhmx temp : zgzjjhmxList) {
                temp.setZgzjjh(z);
                zgzjjhmxDao.save(temp);
            }
        }
    }

    @Transactional
    public void editTpzjjh(Tpzjjh tpzjjh, List<Tpzjjhmx> tpzjjhmxList) {
        Tpzjjh t = tpzjjhDao.get(tpzjjh.getId());
//        t.setJhnf(tpzjjh.getJhnf());
//        t.setJhyf(tpzjjh.getJhyf());
        t.setSbje(sumTpzjjh(tpzjjhmxList));
        t.setLastUpdated(new Date());
        //保存统配资金计划
        tpzjjhDao.save(t);

        for (Tpzjjhmx mx : t.getTpzjjhmxList()) {
            mx.setLastUpdated(new Date());
            mx.setDeleted(1);
            tpzjjhmxDao.save(mx);
        }

        if (tpzjjhmxList != null) {
            for (Tpzjjhmx temp : tpzjjhmxList) {
                temp.setTpzjjh(tpzjjh);
                tpzjjhmxDao.save(temp);
            }
        }
    }
//计算资金计划的总金额
    public Double sumZgsbje(List<Zgzjjhmx> zgzjjhmxList){
        if (zgzjjhmxList == null) return 0.00;
        Double sum = 0.0;
        for(Zgzjjhmx zgzjjhmx : zgzjjhmxList){
            sum += zgzjjhmx.getFkje().doubleValue();
        }
        return sum;
    }

    public Double sumTpzjjh(List<Tpzjjhmx> tpzjjhmxList){
        if (tpzjjhmxList == null) return 0.00;
        Double sum = 0.0;
        for(Tpzjjhmx tgzjjhmx : tpzjjhmxList){
            sum += tgzjjhmx.getFkje().doubleValue();
        }
        return sum;
    }

    @Transactional
    public void deleteZgzjjh(Long id) {
        Zgzjjh z = zgzjjhDao.get(id);
        for (Zgzjjhmx mx : z.getZgzjjhmxList()) {
            mx.setDeleted(1);
            mx.setLastUpdated(new Date());
        }
        z.setDeleted(1);
        zgzjjhDao.save(z);
    }

    @Transactional
    public void deleteTpzjjh(Long id) {
        Tpzjjh t = tpzjjhDao.get(id);
        for (Tpzjjhmx mx : t.getTpzjjhmxList()) {
            mx.setDeleted(1);
            mx.setLastUpdated(new Date());
        }
        t.setDeleted(1);
        tpzjjhDao.save(t);
    }

    @Transactional
    public Zgzjjh findZgzjjhDetailByProcessInstance(String processInstanceId) {
        return this.zgzjjhDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    @Transactional
    public Tpzjjh findTpzjjhDetailByProcessInstance(String processInstanceId) {
        return this.tpzjjhDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setZgzjjhDao(ZgzjjhDao zgzjjhDao) {
        this.zgzjjhDao = zgzjjhDao;
    }

    @Autowired
    public void setTpzjjhDao(TpzjjhDao tpzjjhDao) {
        this.tpzjjhDao = tpzjjhDao;
    }

    @Autowired
    public void setZjjhDao(ZjjhDao zjjhDao) {
        this.zjjhDao = zjjhDao;
    }

    @Autowired
    public void setZgzjjhmxDao(ZgzjjhmxDao zgzjjhmxDao) {
        this.zgzjjhmxDao = zgzjjhmxDao;
    }

    @Autowired
    public void setTpzjjhmxDao(TpzjjhmxDao tpzjjhmxDao) {
        this.tpzjjhmxDao = tpzjjhmxDao;
    }

    @Autowired
    public void setTpwzjhmxDao(TpwzjhmxDao tpwzjhmxDao) {
        this.tpwzjhmxDao = tpwzjhmxDao;
    }


}
